package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 添加套餐
     * SetmealDto = Setmeal + List<SetmealDish>
     */
    @PostMapping
    @CacheEvict(value = "setmeal", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 分页条件查询
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pageInfo = setmealService.pageSetmealDto(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 批量删除套餐
     */
    @DeleteMapping
    @CacheEvict(value = "setmeal", allEntries = true)
    public R<String> deleteByIds(Long[] ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }


    /**
     * 启售，停售状态
     */
    @PostMapping("status/{status}")
    @CacheEvict(value = "setmeal", allEntries = true)
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        //批量修改状态
        // update dish set status=? where id in ( ids )
        setmealService.lambdaUpdate()
                .set(Setmeal::getStatus, status) //修改值
                .in(Setmeal::getId, ids)       //条件部分
                .update();                  //更新操作
        return R.success("修改成功");
    }

    /**
     * 根据id查询套餐详情信息（包含多个菜品）
     */
    @GetMapping("{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @CacheEvict(value = "setmeal", allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        //保存菜品，菜品口味数据
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 处理根据条件查询套餐信息
     *
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "setmeal", key = "#categoryId", unless = "#result.data.size()=0")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        List<Setmeal> list = setmealService.lambdaQuery()
                .eq(categoryId != null, Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status)
                .list();
        return R.success(list);
    }


    /**
     * 处理根据套餐id查询对应菜品信息的请求
     *
     * @param setmealId
     * @return
     */
    @GetMapping("/dish/{setmealId}")
    public R findById(@PathVariable Long setmealId) {
        List<DishDto> dishDtoList = dishService.findById(setmealId);
        return R.success(dishDtoList);
    }
}
